/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.project22_editingmediaplayer;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.audiofx.Equalizer;
import android.media.audiofx.Visualizer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;

import com.example.project22_editingmediaplayer.resource.SongResource;

public class AudioFxDemo extends Activity {
	private static final String TAG = "AudioFxDemo";

	private static final float VISUALIZER_HEIGHT_DIP = 50f;

	private Visualizer mVisualizer;
	private Equalizer mEqualizer;

	private Menu menu1;

	int isfirst = 0;

	private LinearLayout mLinearLayout;
	private VisualizerView mVisualizerView;
	private TextView mStatusTextView;
	String pref = "equalizerStatus";
	SeekBar bar;

	private short minEQLevel;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		setVolumeControlStream(AudioManager.STREAM_MUSIC);

		mStatusTextView = new TextView(this);

		mLinearLayout = new LinearLayout(this);
		mLinearLayout.setOrientation(LinearLayout.VERTICAL);
		mLinearLayout.addView(mStatusTextView);

		setContentView(mLinearLayout);
		Log.d(TAG, "MediaPlayer audio session ID: "
				+ MainActivity.mService.getMediaPlayer().getAudioSessionId());

		setupVisualizerFxAndUI();
		setupEqualizerFxAndUI();

		mVisualizer.setEnabled(true);

		MainActivity.mService.getMediaPlayer().setOnCompletionListener(
				new MediaPlayer.OnCompletionListener() {
					public void onCompletion(MediaPlayer mediaPlayer) {
						mVisualizer.setEnabled(false);
					}
				});
if (MainActivity.mService.getMediaPlayer().isPlaying()) {
	MainActivity.mService.getMediaPlayer().start();
}
		
		mStatusTextView.setText("Playing audio...");

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		switch (SongResource.isitem) {
		case 1:
			setequalizer_initial(SongResource.pop);
			break;
		case 2:
			setequalizer_initial(SongResource.rock);
			break;
		case 3:
			setequalizer_initial(SongResource.jazz);
			break;
		case 4:
			setequalizer_initial(SongResource.classic);
			break;
		case 5:
			setequalizer_initial(SongResource.folk);
			break;
		case 6:
			setequalizer_initial(SongResource.normal);
			break;
		default:

			break;
		}

		isfirst = 1;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menutypemusic, menu);
		this.menu1 = menu;

		MenuItem v1 = menu1.findItem(R.id.pop);
		MenuItem v2 = menu1.findItem(R.id.rock);
		MenuItem v3 = menu1.findItem(R.id.jazz);
		MenuItem v4 = menu1.findItem(R.id.classic);
		MenuItem v5 = menu1.findItem(R.id.folk);
		MenuItem v6 = menu1.findItem(R.id.nomal);
		switch (SongResource.isitem) {
		case 1:

			v1.setIcon(R.drawable.typemusic_unslect);

			v2.setIcon(R.drawable.typemusic_select);

			v3.setIcon(R.drawable.typemusic_select);

			v4.setIcon(R.drawable.typemusic_select);

			v5.setIcon(R.drawable.typemusic_select);

			v6.setIcon(R.drawable.typemusic_select);

			break;
		case 2:

			v1.setIcon(R.drawable.typemusic_select);

			v2.setIcon(R.drawable.typemusic_unslect);

			v3.setIcon(R.drawable.typemusic_select);

			v4.setIcon(R.drawable.typemusic_select);

			v5.setIcon(R.drawable.typemusic_select);

			v6.setIcon(R.drawable.typemusic_select);

			break;
		case 3:
			v1.setIcon(R.drawable.typemusic_select);

			v2.setIcon(R.drawable.typemusic_select);

			v3.setIcon(R.drawable.typemusic_unslect);

			v4.setIcon(R.drawable.typemusic_select);

			v5.setIcon(R.drawable.typemusic_select);

			v6.setIcon(R.drawable.typemusic_select);

			break;
		case 4:
			v1.setIcon(R.drawable.typemusic_select);

			v2.setIcon(R.drawable.typemusic_select);

			v3.setIcon(R.drawable.typemusic_select);

			v4.setIcon(R.drawable.typemusic_unslect);

			v5.setIcon(R.drawable.typemusic_select);

			v6.setIcon(R.drawable.typemusic_select);

			break;
		case 5:
			v1.setIcon(R.drawable.typemusic_select);

			v2.setIcon(R.drawable.typemusic_select);

			v3.setIcon(R.drawable.typemusic_select);

			v4.setIcon(R.drawable.typemusic_select);

			v5.setIcon(R.drawable.typemusic_unslect);

			v6.setIcon(R.drawable.typemusic_select);

			break;
		case 6:
			v1.setIcon(R.drawable.typemusic_select);

			v2.setIcon(R.drawable.typemusic_select);

			v3.setIcon(R.drawable.typemusic_select);

			v4.setIcon(R.drawable.typemusic_select);

			v5.setIcon(R.drawable.typemusic_select);

			v6.setIcon(R.drawable.typemusic_unslect);

			break;

		case 0:
			v1.setIcon(R.drawable.typemusic_select);

			v2.setIcon(R.drawable.typemusic_select);

			v3.setIcon(R.drawable.typemusic_select);

			v4.setIcon(R.drawable.typemusic_select);

			v5.setIcon(R.drawable.typemusic_select);

			v6.setIcon(R.drawable.typemusic_select);
			break;
		}

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return (true);

		case R.id.pop:
			SongResource.isitem = 1;
			setequalizer(SongResource.pop, R.id.pop);

			break;

		case R.id.rock:
			SongResource.isitem = 2;
			setequalizer(SongResource.rock, R.id.rock);

			break;

		case R.id.jazz:
			SongResource.isitem = 3;
			setequalizer(SongResource.jazz, R.id.jazz);

			break;
		case R.id.classic:
			SongResource.isitem = 4;
			setequalizer(SongResource.classic, R.id.classic);

			break;
		case R.id.folk:
			SongResource.isitem = 5;
			setequalizer(SongResource.folk, R.id.folk);

			break;
		case R.id.nomal:
			SongResource.isitem = 6;
			setequalizer(SongResource.normal, R.id.nomal);

			break;
		/*
		 * default: // SongResource.isitem=0; Toast.makeText(getBaseContext(),
		 * "menu bbb", Toast.LENGTH_SHORT).show(); break;
		 */

		}

		return super.onOptionsItemSelected(item);
	}

	private void setupEqualizerFxAndUI() {
		
		mEqualizer = new Equalizer(0, MainActivity.mService.getMediaPlayer()
				.getAudioSessionId());
		mEqualizer.setEnabled(true);

		TextView eqTextView = new TextView(this);
		eqTextView.setText("Equalizer:");
		mLinearLayout.addView(eqTextView);

		short bands = mEqualizer.getNumberOfBands();
		// Toast.makeText(getBaseContext(), ""+ bands ,
		// Toast.LENGTH_SHORT).show();

		minEQLevel = mEqualizer.getBandLevelRange()[0];
		final short maxEQLevel = mEqualizer.getBandLevelRange()[1];

		for (short i = 0; i < bands; i++) {
			final short band = i;
			// Toast.makeText(getBaseContext(), ""+ band ,
			// Toast.LENGTH_SHORT).show();
			TextView freqTextView = new TextView(this);
			freqTextView.setLayoutParams(new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT));
			freqTextView.setGravity(Gravity.CENTER_HORIZONTAL);
			freqTextView.setText((mEqualizer.getCenterFreq(band) / 1000)
					+ " Hz");
			mLinearLayout.addView(freqTextView);

			LinearLayout row = new LinearLayout(this);
			row.setOrientation(LinearLayout.HORIZONTAL);

			TextView minDbTextView = new TextView(this);
			minDbTextView.setLayoutParams(new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT));
			// minDbTextView.setId(i);
			minDbTextView.setText((minEQLevel / 100) + " dB");

			TextView maxDbTextView = new TextView(this);
			maxDbTextView.setLayoutParams(new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT));
			maxDbTextView.setText((maxEQLevel / 100) + " dB");

			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			layoutParams.weight = 1;
			bar = new SeekBar(this);
			bar.setLayoutParams(layoutParams);
			bar.setMax(maxEQLevel - minEQLevel);
			bar.setProgress(mEqualizer.getBandLevel(band));
			bar.setId(i);
			
			bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
				public void onProgressChanged(SeekBar seekBar, int progress,
						boolean fromUser) {
					/* Toast.makeText(getBaseContext(), ""+ progress ,
					 Toast.LENGTH_SHORT).show();*/
					mEqualizer.setBandLevel(band,
							(short) (progress + minEQLevel));
					
					if (isfirst == 1) {
						if (SongResource.flag == 0) {
							SongResource.isitem = 0;
						}

					}

					
				}

				public void onStartTrackingTouch(SeekBar seekBar) {
				}

				public void onStopTrackingTouch(SeekBar seekBar) {
				}
			});

			row.addView(minDbTextView);
			row.addView(bar);
			row.addView(maxDbTextView);

			mLinearLayout.addView(row);
		}
		
	}



	private void setupVisualizerFxAndUI() {
		
		mVisualizerView = new VisualizerView(this);
		mVisualizerView.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				(int) (VISUALIZER_HEIGHT_DIP * getResources()
						.getDisplayMetrics().density)));
		mLinearLayout.addView(mVisualizerView);

		
		mVisualizer = new Visualizer(MainActivity.mService.getMediaPlayer()
				.getAudioSessionId());
				mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
		
		mVisualizer.setDataCaptureListener(
				new Visualizer.OnDataCaptureListener() {
					public void onWaveFormDataCapture(Visualizer visualizer,
							byte[] bytes, int samplingRate) {
						mVisualizerView.updateVisualizer(bytes);
					}

					public void onFftDataCapture(Visualizer visualizer,
							byte[] bytes, int samplingRate) {
					}
				}, Visualizer.getMaxCaptureRate() / 2, true, false);
	}

	@Override
	protected void onPause() {
		super.onPause();

		if (isFinishing() && MainActivity.mService.getMediaPlayer() != null) {
			mVisualizer.release();
			
		}
	}

	public void setequalizer_initial(int type[]) {

		for (int i = 0; i < 5; i++) {
			mEqualizer.setBandLevel((short) i, (short) (type[i] + minEQLevel));

			SeekBar b = (SeekBar) AudioFxDemo.this.findViewById(i);
			b.setProgress(type[i]);
		}
	}

	public void setequalizer(int type[], int itemid) {
		SongResource.flag = 1;

		for (int i = 0; i < 5; i++) {
			mEqualizer.setBandLevel((short) i, (short) (type[i] + minEQLevel));

			SeekBar b = (SeekBar) AudioFxDemo.this.findViewById(i);
			b.setProgress(type[i]);
		}

		MenuItem v1 = menu1.findItem(R.id.pop);
		MenuItem v2 = menu1.findItem(R.id.jazz);
		MenuItem v3 = menu1.findItem(R.id.rock);
		MenuItem v4 = menu1.findItem(R.id.folk);
		MenuItem v5 = menu1.findItem(R.id.classic);
		MenuItem v6 = menu1.findItem(R.id.nomal);
		switch (itemid) {
		case R.id.pop:

			v1.setIcon(R.drawable.typemusic_unslect);

			v2.setIcon(R.drawable.typemusic_select);

			v3.setIcon(R.drawable.typemusic_select);

			v4.setIcon(R.drawable.typemusic_select);

			v5.setIcon(R.drawable.typemusic_select);

			v6.setIcon(R.drawable.typemusic_select);

			break;
		case R.id.jazz:

			v1.setIcon(R.drawable.typemusic_select);

			v2.setIcon(R.drawable.typemusic_unslect);

			v3.setIcon(R.drawable.typemusic_select);

			v4.setIcon(R.drawable.typemusic_select);

			v5.setIcon(R.drawable.typemusic_select);

			v6.setIcon(R.drawable.typemusic_select);

			break;
		case R.id.rock:
			v1.setIcon(R.drawable.typemusic_select);

			v2.setIcon(R.drawable.typemusic_select);

			v3.setIcon(R.drawable.typemusic_unslect);

			v4.setIcon(R.drawable.typemusic_select);

			v5.setIcon(R.drawable.typemusic_select);

			v6.setIcon(R.drawable.typemusic_select);

			break;
		case R.id.folk:
			v1.setIcon(R.drawable.typemusic_select);

			v2.setIcon(R.drawable.typemusic_select);

			v3.setIcon(R.drawable.typemusic_select);

			v4.setIcon(R.drawable.typemusic_unslect);

			v5.setIcon(R.drawable.typemusic_select);

			v6.setIcon(R.drawable.typemusic_select);

			break;
		case R.id.classic:
			v1.setIcon(R.drawable.typemusic_select);

			v2.setIcon(R.drawable.typemusic_select);

			v3.setIcon(R.drawable.typemusic_select);

			v4.setIcon(R.drawable.typemusic_select);

			v5.setIcon(R.drawable.typemusic_unslect);

			v6.setIcon(R.drawable.typemusic_select);

			break;
		case R.id.nomal:
			v1.setIcon(R.drawable.typemusic_select);

			v2.setIcon(R.drawable.typemusic_select);

			v3.setIcon(R.drawable.typemusic_select);

			v4.setIcon(R.drawable.typemusic_select);

			v5.setIcon(R.drawable.typemusic_select);

			v6.setIcon(R.drawable.typemusic_unslect);

			break;

		default:
			v1.setIcon(R.drawable.typemusic_select);

			v2.setIcon(R.drawable.typemusic_select);

			v3.setIcon(R.drawable.typemusic_select);

			v4.setIcon(R.drawable.typemusic_select);

			v5.setIcon(R.drawable.typemusic_select);

			v6.setIcon(R.drawable.typemusic_select);
			break;
		}

		SongResource.flag = 0;
	}

}

/**
 * A simple class that draws waveform data received from a
 * {@link Visualizer.OnDataCaptureListener#onWaveFormDataCapture }
 */
class VisualizerView extends View {
	private byte[] mBytes;
	private float[] mPoints;
	private Rect mRect = new Rect();

	private Paint mForePaint = new Paint();

	public VisualizerView(Context context) {
		super(context);
		init();
	}

	private void init() {
		mBytes = null;

		mForePaint.setStrokeWidth(1f);
		mForePaint.setAntiAlias(true);
		mForePaint.setColor(Color.rgb(0, 128, 255));
	}

	public void updateVisualizer(byte[] bytes) {
		mBytes = bytes;
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		if (mBytes == null) {
			return;
		}

		if (mPoints == null || mPoints.length < mBytes.length * 4) {
			mPoints = new float[mBytes.length * 4];
		}

		mRect.set(0, 0, getWidth(), getHeight());

		for (int i = 0; i < mBytes.length - 1; i++) {
			mPoints[i * 4] = mRect.width() * i / (mBytes.length - 1);
			mPoints[i * 4 + 1] = mRect.height() / 2
					+ ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128;
			mPoints[i * 4 + 2] = mRect.width() * (i + 1) / (mBytes.length - 1);
			mPoints[i * 4 + 3] = mRect.height() / 2
					+ ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2)
					/ 128;
		}

		canvas.drawLines(mPoints, mForePaint);
	}

}
